﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace OnTheWay.Tool
{
    public class RegistryHelper
    {
        public static String SOFTKEY = "HuiZhongOA";
        /// <summary>
        /// 从注册表中读取一项配置
        /// </summary>
        /// <param name="configItemName">配置项名称</param>
        /// <returns>
        /// 成功读取返回对应的配置项的值
        /// 否则返回null
        /// </returns>
        public static string GetConfigItemValue(string configItemName)
        {
            try
            {
                RegistryKey localMachine = Registry.CurrentUser;
                RegistryKey software = localMachine.OpenSubKey("SOFTWARE", true);
                RegistryKey arcDessignSSClientConfig = software.OpenSubKey(SOFTKEY, true);
                return arcDessignSSClientConfig.GetValue(configItemName).ToString();
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 添加配置项
        /// </summary>
        /// <param name="configItemName">配置项名称</param>
        /// <param name="configItemValue">配置项值</param>
        /// <returns>
        /// 添加成功返回true
        /// 添加失败返回false
        /// </returns>
        public static bool AddConfigItemValue(string configItemName, string configItemValue)
        {
            try
            {
                RegistryKey localMachine = Registry.CurrentUser;
                RegistryKey software = localMachine.OpenSubKey("SOFTWARE", true);
                RegistryKey arcDessignSSClientConfig = software.CreateSubKey(SOFTKEY);
                arcDessignSSClientConfig.SetValue(configItemName, configItemValue);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 删除配置项
        /// </summary>
        /// <param name="configItemName">配置项名称</param>
        /// <returns>
        /// 删除成功返回true
        /// 删除失败返回false
        /// </returns>
        public static bool DeleteConfigItem(string configItemName)
        {
            try
            {
                string[] itemChildren;
                RegistryKey localMachine = Registry.CurrentUser;
                RegistryKey software = localMachine.OpenSubKey("SOFTWARE", true);
                RegistryKey arcDessignSSClientConfig = software.OpenSubKey(SOFTKEY, true);
                itemChildren = arcDessignSSClientConfig.GetSubKeyNames();
                foreach (string itemKey in itemChildren)
                {
                    if (itemKey == configItemName)
                        arcDessignSSClientConfig.DeleteSubKeyTree(configItemName);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 判断配置项是否存在
        /// </summary>
        /// <param name="configItemName">配置项名称</param>
        /// <returns>
        /// 存在返回true
        /// 不存在返回false
        /// </returns>
        public static bool IsConfigItemExit(string configItemName)
        {
            bool _exit = false;
            string[] subkeyNames;
            RegistryKey hkml = Registry.CurrentUser;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey(SOFTKEY, true);
            subkeyNames = aimdir.GetSubKeyNames();
            foreach (string keyName in subkeyNames)
            {
                if (keyName == configItemName)
                {
                    _exit = true;
                    return _exit;
                }
            }
            return _exit;
        }
    }
}
